var Player = require("../../domain/player")
var skillAttack = require("../../domain/skillAttack")
var redisClient = require('../../dao/redis/redisClient')
var playerCon = require('../../domain/playerCon')
var Bag = require("../../domain/bag")
module.exports = function(app) {
    app.all('/player/*', function(req, res, next) {
        //是否选择角色
        var playerid = req.session.playerid
        if (playerid) {
            var player = global.playerTree.get(playerid)
            if (player) {
                req.player = player
                next()
            } else {
                //获取角色属性
                redisClient.get('p:' + playerid, function(err, redisPlayer) {
                    player = JSON.parse(redisPlayer)
                    player = new Player(player)
                    //获取角色物品信息,背包
                    redisClient.mget('i:' + playerid, 'b:' + playerid, function(err, ret) {
                        if (ret[0]) {
                            //物品信息
                            player.itemInfo = JSON.parse(ret[0])
                        } else {
                            player.itemInfo = {}
                        }
                        if (ret[1]) {
                            //背包
                            player.bag = new Bag(JSON.parse(ret[1]))
                        } else {
                            player.bag = new Bag({})
                        }
                        global.playerTree.put(playerid, player)
                        player.calcAttri()
                        req.player = player
                        next()
                    })
                })
            }
        } else {
            res.send({
                error: 102
            })
            return
        }
    })

    app.post('/player/delPlayer', function(req, res) {
        playerCon.delPlayer(req.session.userid, req.session.playerid)
        req.session.playerid = null
        res.send({
            error: 0
        })
    })

    app.post('/player/lvlPlayer', function(req, res) {
        playerCon.lvlPlayer(req.player)
        res.send({
            error: 0,
            player: player
        })
    })

    app.post('/player/lvlPlayerByexp', function(req, res) {
        playerCon.lvlPlayerByexp(req.player)
        res.send({
            error: 0,
            player: req.player
        })
    })

    app.post('/player/selectMap', function(req, res) {
        var mapId = req.body.mapId
        req.player.mapId = mapId
        req.player.savePlayer()
        res.send({
            error: 0,
            mapId: mapId
        })
        return
    })

    app.post('/player/battleOnMap', function(req, res) {
        var fightRecord = playerCon.battleOnMap(req.player)
        res.send({
            error: 0,
            fightRecord: fightRecord
        })
    })

    app.post('/player/getPlayer', function(req, res) {
        res.send({
            error: 0,
            player: req.player
        })
        return
    })

    app.post('/player/getPlayerBag', function(req, res) {
        res.send({
            error: 0,
            bag: req.player.bag
        })
        return
    })

    app.post('/player/getFightRecord', function(req, res) {
        redisClient.lrange('lf:' + req.player.id, 0, 10, function(err, ret) {
            res.send({
                error: 0,
                fightRecord: ret
            })
        })
    })

    app.post('/player/getCommoditylist', function(req, res) {
       
    })
}